﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace translateCTPTDSpi
{
    public partial class Form1 : Form
    {
        string[] entryPointStrArr;
 
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string line = this.textBox1.Text;
            line = line.Replace("{};","");
            int leftParIndex = line.IndexOf('(');
            int rightParIndex = line.IndexOf(')');
            string oldName = line.Substring(0, leftParIndex);

            string sig = line.Substring(leftParIndex,rightParIndex-leftParIndex+1);
            oldName = oldName.Replace("virtual void ", "");
            string TDName = "TD" + oldName;
            string tdName = "td" + oldName;


            string line1 = "typedef void(WINAPI* " + TDName + ")" + sig + ";";
            string line2 = TDName + " " + tdName + " = 0;";
            string line3 = "CTPTD_API void reg" + oldName + "(" + TDName + " func) {";
            string line4 = "\t" + tdName + " = func;";
            string line5 = "}";
            string line6 = "void CCTPTD::" + oldName + sig + "{";
            string line7 = "\tif (" + tdName + " != NULL) {";
            //处理签名
            string line8 = "\t\t"+tdName+"(";
            string temp = sig.Replace("(","");
            temp = temp.Replace(")","");
            string [] sigArray = temp.Split(',');
            for (int i = 0;i < sigArray.Length;i++) {
                string item = sigArray[i];
                item = item.Trim();
                string[] itemName = item.Split(' ');
                string part2 = itemName[1];
                part2 = part2.Replace("*","");
                if (i != sigArray.Length-1) {
                    line8 = line8 + part2 + ",";
                }
                else {
                    line8 = line8+ part2 + ");";
                }
            }


            this.textBox2.Text = line1 + "\r\n" + line2 + "\r\n" + line3 + "\r\n" + line4 + "\r\n" + line5 + "\r\n" + line6 + "\r\n" + line7 + "\r\n" + line8 + "\r\n\t}\r\n};"; 

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.entryPointStrArr = File.ReadAllLines(@"C:\CTP_CSHARP\CCTPTD.txt");


        }

        private void button3_Click(object sender, EventArgs e)
        {
            string oldLine = this.textBox1.Text;
            int leftParIndex = oldLine.IndexOf('(');
            int rightParIndex = oldLine.IndexOf(')');
            string oldName = oldLine.Substring(0,leftParIndex);
            string signature = oldLine.Substring(leftParIndex + 1, rightParIndex - leftParIndex - 1);
            
            //处理第1句
            string[] temp = oldName.Split(' ');
            oldName = temp[2];
            string type = temp[1];
            string key = "?"+oldName + "@";
            string entryPointStr = "";
                //查找OldName
            for (int i = 0;i < this.entryPointStrArr.Length;i++) {
                if (this.entryPointStrArr[i].Contains(key)) {
                    entryPointStr = this.entryPointStrArr[i];
                    break;
                }
            }
            string line1 = "\t\t[DllImport(CTPTD_Addr, EntryPoint = \"" + entryPointStr + "\" , CallingConvention = CallingConvention.Cdecl)]";

            //处理第2句
            if (signature.Contains("*"))
            {
                signature = signature.Replace("*", "");
                signature = "ref " + signature;
            }
            string line2 = "\t\tpublic static extern " + type + " " + oldName + "(" + signature + ");";
            this.textBox2.Text = line1 + "\r\n" + line2;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string line = this.textBox1.Text;
            string[] temp = line.Split(new[] { ")("},StringSplitOptions.RemoveEmptyEntries);
            string oldName = temp[0];
            string sig = temp[1];
           
            //name
            temp = oldName.Split(' ');
            oldName = temp[2];

            string line1 = "public delegate void " + oldName + "(";
            //signature
            sig = sig.Replace(");", "");
            string[] sigItem = sig.Split(',');
            for (int i = 0; i < sigItem.Length; i++)
            {
                string tempItem = sigItem[i];
                if (tempItem.Contains('*'))
                {
                    tempItem = tempItem.Replace("*", "");
                    tempItem = "ref " + tempItem;
                }
                if (i < sigItem.Length-1) {
                    line1 = line1 + tempItem + ",";
                }
                else
                {
                    line1 = line1 + tempItem + ");";
                }

            }

            //第二句
            string regName = oldName.Replace("TD", "reg");
            string key = "?" + regName + "@";
            string entryPointStr = "";
            //查找regName
            for (int i = 0; i < this.entryPointStrArr.Length; i++)
            {
                if (this.entryPointStrArr[i].Contains(key))
                {
                    entryPointStr = this.entryPointStrArr[i];
                    break;
                }
            }
            string line2 = "\t\t[DllImport(CTPTD_Addr, EntryPoint = \"" + entryPointStr + "\" , CallingConvention = CallingConvention.Cdecl)]";

            //第三句
            string line3 = "\t\tpublic static extern void " + regName + "(" + oldName + " td);";
            this.textBox2.Text = "\t\t" + line1 + "\r\n" + line2 + "\r\n" + line3;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string line = this.textBox1.Text;
            int leftParIndex = line.IndexOf('(');
            int rightParIndex = line.IndexOf(')');
            string name = line.Substring(0, leftParIndex);
            string[] tempName = name.Split(' ');
            name = tempName[tempName.Length - 1];
            string sig = line.Substring(leftParIndex,rightParIndex-leftParIndex-1);
            string[] sigArr = sig.Split(',');
            string firstParam = sigArr[0].Trim();

            string[] firstParamArr = firstParam.Split(' ');
            firstParam = firstParamArr[1];
            string firstParmInput = firstParamArr[2];
            string firstParamCut = firstParam.Replace("CThostFtdc","");
            //

            string eventTypeName = "E_" + name;
            string argsName = name + "Args";

            string line0 = eventTypeName;
            string line1 = "\tpublic class " + argsName + " : CTPEventArgs";
            string line2 = "\t{";
            string line3 = "\t\tpublic readonly " + firstParam + " " + firstParamCut+";";
            string line4 = "\t\tpublic " + argsName + sig.Replace("ref","") + ")";
            string line5 = "\t\t{";
            string line6 = "\t\t\tthis.type = CTPEventType." + eventTypeName + ";";
            string line7 = "\t\t\tthis." + firstParamCut + " = " + firstParmInput + ";";
            string line8 = "\t\t\tthis.RspInfoField = pRspInfo;";
            string line9 = "\t\t\tthis.nRequestID = nRequestID;";
            string line10 = "\t\t\tthis.bIsLast = bIsLast;";
            string line11 = "\t\t}";
            string line12 = "\t}";


            this.textBox2.Text = line0 + "\r\n" + line1 + "\r\n" + line2 + "\r\n" + line3 + "\r\n" + line4 + "\r\n" + line5 + "\r\n" + line6 + "\r\n" + line7 + "\r\n" + line8 + "\r\n" + line9 + "\r\n" + line10 + "\r\n" + line11 + "\r\n" + line12;
        }
    }
}
